using Newtonsoft.Json;
using System.Text.RegularExpressions;

namespace Infrastructure.Utils
{
    #region 字符串长度比较器
    /// <summary>
    /// 字符串长度比较器
    /// </summary>
    public class ComparerStrLength : IComparer<string>
    {
        private bool _b { get; set; }
        public ComparerStrLength(bool b = true)
        {
            _b = b;
        }
        public int Compare(string x, string y)
        {
            if (_b) return x.Length - y.Length;
            return y.Length - x.Length;
        }
    }
    #endregion

    #region 字符串比较器[类通讯录]
    /// <summary>
    /// 字符串比较器[类通讯录eg{"1","022","a","A"}]
    /// </summary>
    public class ComparerStr : IComparer<string>
    {
        public int Compare(string a, string b)
        {

            var x = Regex.Match(a ?? "", @"\d+").Value;
            var y = Regex.Match(b ?? "", @"\d+").Value;
            if (x.Length == 0 && y.Length == 0) return a.CompareTo(b);

            if (x.Length == 0 || y.Length == 0)
            {
                if (x.Length > 0 && y.Length == 0) return -1;
                if (x.Length == 0 && y.Length > 0) return 1;
            }
            int xInt = Convert.ToInt32(x);
            int yInt = Convert.ToInt32(y);
            return xInt - yInt;
        }
    }
    #endregion

    #region 实体是否相同
    // 示例：var tmp= lst23.Except(lst22,new CompareEntity<PCModelInfo>());
    public class CompareEntity<T> : IEqualityComparer<T>
    {
        public bool Equals(T x, T y)
        {
            return JsonConvert.SerializeObject(x).Equals(JsonConvert.SerializeObject(y));
        }
        public int GetHashCode(T obj)
        {
            return 0;
        }
    }
    #endregion

    #region 获取实体不同属性字典集合
    // 示例：var distinct=ComparerHelper.Comparer(item,styleOrigin );
    public class ComparerHelper
    {
        /// <summary>
        /// 以x为基础，找出y中没有的x的属性/值集合;eg_x:{id=1,age=18} eg_y:{id=1,name="zs"}差异属性：age=18
        /// </summary>
        public static Dictionary<string, object> Comparer<T>(T x, T y)
        {
            var dic = new Dictionary<string, object>();
            var listPro = x.GetType().GetProperties();
            foreach (var item in listPro)
            {
                var xValue = item.GetValue(x);
                if (y == null)
                {
                    dic.Add(item.Name, xValue);
                    continue;
                }
                var yValue = y.GetType().GetProperty(item.Name).GetValue(y);
                if (!Equals(yValue, item.GetValue(x))) dic.Add(item.Name, xValue);
            }
            return dic;
        }
    }
    #endregion

    #region 按指定数组排序,数组内优先
    /// <summary>
    /// 按指定数组排序,数组内优先
    /// </summary>
    public class ComparerByArray<T> : IComparer<T>
    {
        //使用：
        //var orderListByName = list.OrderBy(s => s.Name, new ComparerByArray<string>(new string[] {"a","c","b"})).ToList();
        //var orderListValue = list.OrderBy(s => s.Value, new ComparerByArray<int>(new int[] {1,3,2})).ToList();
        private T[] _arr { get; set; }
        public ComparerByArray(T[] arr)
        {
            _arr = arr;
        }
        public int Compare(T x, T y)
        {
            var index1 = Array.IndexOf(_arr, x);
            var index2 = Array.IndexOf(_arr, y);
            if (index2 > -1 && index1 > -1) return index1 - index2;
            return index2 - index1;
        }
    }
    #endregion
}